/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.documents.service.knowledge;

import com.documents.common.core.PageBean;
import com.documents.common.core.service.BaseSrv;
import com.documents.pojo.Country;
import com.documents.pojo.Product;
import com.documents.pojo.Zhishi;
import com.documents.service.country.CountryService;
import com.documents.service.product.ProductService;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class ZhishiService extends BaseSrv<Zhishi, Integer> {

    private ProductService productService;
    private CountryService countryService;

    public CountryService getCountryService() {
        return countryService;
    }

    public void setCountryService(CountryService countryService) {
        this.countryService = countryService;
    }

    public ProductService getProductService() {
        return productService;
    }

    public void setProductService(ProductService productService) {
        this.productService = productService;
    }

    public PageBean getPageBean(int page, int pageSize, Zhishi knowledge) {
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);
        pageBean.setCurrentPage(page);
        String condition = "";
        Object[] parameters = new Object[10];
        int paramIndex = 0;
        if (knowledge != null) {

            if (knowledge.getCountry() > 0) {
                if (condition.trim().length() == 0) {
                    condition += "country_code like ?";
                } else {
                    condition += "and country_code like ?";
                }
                Country cc = countryService.get(knowledge.getCountry());
                if (cc != null) {
                    parameters[paramIndex] = cc.getCode() + "%";
                    paramIndex++;
                }


            }
//            if (knowledge.getCountryName() != null && !knowledge.getCountryName().equals("")) {
//                if (condition.trim().length() == 0) {
//                    condition += "countryName=?";
//                } else {
//                    condition += "and countryName=?";
//                }
//
//                parameters[paramIndex] = knowledge.getCountryName();
//                paramIndex++;
//            }

            if (knowledge.getProduct() > 0) {
                if (condition.trim().length() == 0) {
                    condition += " product_code like ?";

                } else {
                    condition += " and product_code like ?";
                }
                Product pp = productService.get(knowledge.getProduct());
                if (pp != null) {
                    parameters[paramIndex] = pp.getCode() + "%";
                    paramIndex++;
                }

            }

//
//            if (knowledge.getProductName() != null && !knowledge.getProductName().equals("")) {
//                if (condition.trim().length() == 0) {
//                    condition += " productName=?";
//
//                } else {
//                    condition += " and productName=?";
//                }
//                parameters[paramIndex] = knowledge.getProductName();
//                paramIndex++;
//            }

            if (knowledge.getContentType() > 0) {
                if (condition.trim().length() == 0) {
                    condition += " contentType=?";

                } else {
                    condition += " and contentType=?";
                }
                parameters[paramIndex] = knowledge.getContentType();
                paramIndex++;
            }

            if (knowledge.getContentTypeName() != null && !knowledge.getContentTypeName().equals("")) {

                if (condition.trim().length() == 0) {
                    condition += " contentTypeName=?";

                } else {
                    condition += " and contentTypeName=?";
                }
                parameters[paramIndex] = knowledge.getContentTypeName();
                paramIndex++;
            }
            if (knowledge.getInstitution() > 0) {
                if (condition.trim().length() == 0) {
                    condition += " institution=?";

                } else {
                    condition += " and institution=?";
                }
                parameters[paramIndex] = knowledge.getInstitution();
                paramIndex++;
            }
            if (knowledge.getInstitutionName() != null && !knowledge.getInstitutionName().equals("")) {

                if (condition.trim().length() == 0) {
                    condition += " institutionName=?";

                } else {
                    condition += " and institutionName=?";
                }
                parameters[paramIndex] = knowledge.getInstitutionName();
                paramIndex++;
            }

            if (knowledge.getKnowledgeArea() > 0) {
                if (condition.trim().length() == 0) {
                    condition += " knowledgeArea=?";

                } else {
                    condition += " and knowledgeArea=?";
                }
                parameters[paramIndex] = knowledge.getKnowledgeArea();
                paramIndex++;
            }
            if (knowledge.getKnowledgeAreaName() != null && !knowledge.getKnowledgeAreaName().equals("")) {
                if (condition.trim().length() == 0) {
                    condition += " knowledgeAreaName=?";

                } else {
                    condition += " and knowledgeAreaName=?";
                }
                parameters[paramIndex] = knowledge.getKnowledgeAreaName();
                paramIndex++;
            }
            Long allRow = this.findTotalCount(condition, parameters);
            int totalPage = PageBean.countTotalPage(pageSize, allRow.intValue()); // 总页数
            pageBean.setAllRow(allRow.intValue());
            pageBean.setTotalPage(totalPage);
            List<Zhishi> list;

            if (page == 1) {
                list = this.findWithPaging(condition, parameters, "id", page - 1, pageSize);
            } else {
                list = this.findWithPaging(condition, parameters, "id", (page - 1) * pageSize, pageSize);
            }


            pageBean.setList(list);

        } else {
            Long allRow = this.findTotalCount("", null);
            int totalPage = PageBean.countTotalPage(pageSize, allRow.intValue()); // 总页数
            pageBean.setAllRow(allRow.intValue());
            pageBean.setTotalPage(totalPage);
            List<Zhishi> list;
            if (page == 1) {
                list = this.findWithPaging(condition, parameters, "id", page - 1, pageSize);
            } else {
                list = this.findWithPaging(condition, parameters, "id", (page - 1) * pageSize, pageSize);
            }

            pageBean.setList(list);
        }
        return pageBean;
    }
}
